package com.yx.fx.log.web.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.fx.log.db.entity.Logger;
import com.yx.fx.log.db.service.LoggerService;
import com.yx.fx.log.web.config.LogCacheConfig;
import com.yx.fx.log.web.model.find.FindLoggerPage;
import com.yx.fx.log.web.model.save.SaveLogger;
import com.yx.fx.log.web.model.update.UpdateLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.yx.fx.common.constant.BaseConstant.NINETY_NINE;


/**
 * @author 杨智杰
 * @date 2021/8/11 11:28
 */
@Slf4j
@Service
public class LoggerBusiness {

    @Resource
    private LoggerService loggerService;

    /**
     * 新增
     *
     * @return
     */
    public Boolean save(SaveLogger param) {
        Logger logger = Convert.convert(Logger.class, param);
        return loggerService.save(logger.setCreateTime(DateUtil.now()));
    }

    /**
     * 修改
     *
     * @param param
     * @return
     */
    public Boolean update(UpdateLogger param) {
        return loggerService.updateById(Convert.convert(Logger.class, param));
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    public IPage<Logger> page(FindLoggerPage param) {
        return loggerService.page(new Page<>(param.getCurrent(), param.getSize()),
                new QueryWrapper<Logger>().lambda()
                        .eq(ObjectUtil.isNotNull(param.getId()), Logger::getId, param.getId())
                        .like(StrUtil.isNotBlank(param.getMethod()), Logger::getMethod, param.getMethod())
                        .like(StrUtil.isNotBlank(param.getHeaders()), Logger::getHeaders, param.getHeaders())
                        .like(StrUtil.isNotBlank(param.getRequestBody()), Logger::getRequestBody, param.getRequestBody())
                        .like(StrUtil.isNotBlank(param.getRequestURI()), Logger::getRequestURI, param.getRequestURI())
                        .or(StrUtil.isNotBlank(param.getLoginName()), i ->
                                i.like(Logger::getHeaders, StrUtil.DOT + SecureUtil.md5(param.getLoginName()) + StrUtil.DOT)
                                        .or().like(Logger::getRequestBody, "\"loginName\": \"" + param.getLoginName() + "\""))
                        .ge(StrUtil.isNotBlank(param.getBeginTime()), Logger::getCreateTime, param.getBeginTime())
                        .le(StrUtil.isNotBlank(param.getEndTime()), Logger::getCreateTime, param.getEndTime())
                        .orderByDesc(Logger::getCreateTime)
        );
    }

    /**
     * 删除
     *
     * @param param
     * @return
     */
    public Boolean delete(List<Long> param) {
        return loggerService.removeByIds(param);
    }

    /**
     * 缓存添加日志
     *
     * @param param
     * @return
     */
    public void setCaChe(SaveLogger param) {
        LogCacheConfig.logCache.put(SecureUtil.md5(RandomUtil.randomString(NINETY_NINE) + System.currentTimeMillis()), param);
    }
}
